/**
 * tree转换成数组
 * @author xiufu.wang
 */
import { isArray } from './util'
export function workTree(treeDatas, callback, childrenField, parentNode) {
    if (!treeDatas) {
        return
    }
    treeDatas = isArray(treeDatas) ? treeDatas : [treeDatas]
    for (let i = 0; i < treeDatas.length; i++) {
        const e = treeDatas[i];
        callback(e, parentNode)
        workTree(e[childrenField], callback, childrenField, e)
    }
}
export default function (datas, childrenField = 'children', mapFn) {
    const res = []
    workTree(datas, function (item, pitem) {
        let v = item
        if (typeof mapFn === 'function') {
            const _nv = mapFn(item, pitem)
            if (_nv === null) {
                return
            }
            v = _nv
        }
        res.push(v)
    }, childrenField, null)
    return res;

}